package com.netease.nie.strategy;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.cache.redis.Redis;
import com.netease.nie.constants.CachePrefixConstants;
import com.netease.nie.constants.DisplayTypeConstants;
import com.netease.nie.dto.BannerDto;
import com.netease.nie.model.Banner;
import com.netease.nie.service.BannerService;

/**
 * 
 * @author wwbn3695
 * @createDate July 2rd,2016
 *
 */
@Service
public class BannerNoConditionStrategy extends AbstractBannerStrategy {

	private static final String BANNER_NO_CUSTOM_LIST_PREFIX = CachePrefixConstants.BannerPrefix.BANNER_NO_CUSTOM_LIST_PREFIX;
	
	@Autowired
	private BannerService bannerService;
	
	@Autowired
	private Redis redis;
	
	public List<BannerDto> listBannerDtoByPosition(Long userId, int position) {
		String bannerListKey = BANNER_NO_CUSTOM_LIST_PREFIX + position;
		boolean exists = redis.exists(bannerListKey);
		List<Banner> bannerList = null;
		if (!exists) {
			bannerList = bannerService.queryAllNoCustomBannerByPosition(position);
			if (CollectionUtils.isNotEmpty(bannerList))
				redis.rpush(bannerListKey, bannerList);
		} else
			bannerList = redis.lrange(bannerListKey, 0, -1, Banner.class);
		
		List<BannerDto> bannerDtoList = generateBannerDtoList(bannerList, DisplayTypeConstants.WAP.getCode());
		return bannerDtoList;
	}
	
}
